package com.dl.exam.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.exam.entity.Exam;
import com.dl.exam.entity.Page;
import com.dl.exam.entity.User;
import com.dl.exam.entity.question.Question;
import com.dl.exam.entity.question.SingleQuestion;
import com.dl.exam.entity.question.SubjectiveQuestion;
import com.dl.exam.service.AdminService;
import com.dl.exam.service.ExamService;
import com.dl.exam.service.SingleService;
import com.dl.exam.service.SubjectiveService;
import com.dl.exam.util.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2022-04-22 14:13
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private ExamService examService;
    @Resource
    private AdminService adminService;
    @Resource
    private SingleService singleService;
    @Resource
    private SubjectiveService subjectiveService;

    // 修改用户账号状态
    @GetMapping("/modifyState")
    private Result modifyState(String userId, Integer state) {
        boolean modifyState = adminService.modifyState(userId, state);
        if (modifyState){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }

    // 获得用户信息
    @GetMapping("/getUserAllInfo")
    private Result getUserAllInfo(String userId) {
        User userAllInfo = adminService.getUserAllInfo(userId);
        if (userAllInfo != null){
            return Result.ok("成功").data("user", userAllInfo);
        }else {
            return Result.error("失败");
        }
    }

    // 修改用户角色
    @PostMapping("/modifyUserRole")
    private Result modifyUserRole(@RequestBody JSONObject jsonObject) {
        JSONObject modifyRoleForm = jsonObject.getJSONObject("modifyRoleForm");
        String userId = modifyRoleForm.getString("userId");
        JSONArray roleIdList = modifyRoleForm.getJSONArray("roleIdList");
        boolean modifyUserRole = adminService.modifyUserRole(userId, roleIdList);
        if (modifyUserRole){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }

    // 修改用户信息
    @PostMapping("/modifyUserAllInfo")
    private Result modifyUserAllInfo(@RequestBody JSONObject jsonObject) {
        JSONObject user = jsonObject.getJSONObject("user");
        boolean modifyUserAllInfo = adminService.modifyUserAllInfo(user);
        if (modifyUserAllInfo){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }

    //查询用户所有的考试
    @GetMapping("/historyExam")
    private Result getHistoryExam(String userId, Integer currentPage) {
        Page<Exam> page = examService.getAllExam(userId, currentPage, 12);

        if (page != null){
            return Result.ok("成功").data("page", page);
        }else {
            return Result.error("失败");
        }
    }

    //查询用户所有的考试
    @PostMapping("/addQuestion")
    private Result addQuestion(@RequestBody JSONObject jsonObject) {
        Integer questionType = jsonObject.getInteger("questionType");
        JSONObject questionForm = jsonObject.getJSONObject("questionForm");
        boolean addFlag = false;
        switch (questionType){
            case 1 :
                addFlag = singleService.addQuestion(questionForm.toJavaObject(SingleQuestion.class));break;
            //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            case 3 : addFlag = subjectiveService.addQuestion(questionForm.toJavaObject(SubjectiveQuestion.class));break;
        }

        if (addFlag){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }

    // 查询试题列表
    @PostMapping("/questionList")
    private Result questionList(@RequestBody JSONObject jsonObject) {
        Integer questionType = jsonObject.getInteger("questionType");
        JSONObject condition = jsonObject.getJSONObject("condition");
        Page<?> page = null;
        switch (questionType){
            case 1 :
                page = singleService.getQuestionByCondition(condition);
                break;
            //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            case 3 : page = subjectiveService.getQuestionByCondition(condition);break;
        }
        System.out.println("page = " + page);

        if (page != null){
            return Result.ok("成功").data("page", page);
        }else {
            return Result.error("失败");
        }
    }

    @GetMapping("/getQuestion")
    private Result getQuestionById(Integer questionType, String questionId) {
        Question question = null;
        switch (questionType){
            case 1 :
                question = singleService.getQuestionById(questionId);
                break;
            //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            case 3 : question = subjectiveService.getQuestionById(questionId);break;
        }
        if (question != null){
            return Result.ok("成功").data("question", question);
        }else {
            return Result.error("失败");
        }
    }

    @PostMapping("/modifyQuestion")
    private Result modifyQuestion(@RequestBody JSONObject jsonObject) {
        Integer questionType = jsonObject.getInteger("questionType");
        JSONObject questionForm = jsonObject.getJSONObject("questionForm");

        boolean modifyFlag = false;
        switch (questionType){
            case 1 :
                modifyFlag = singleService.modifyQuestion(questionForm);
                break;
            //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            case 3 : modifyFlag = subjectiveService.modifyQuestion(questionForm);break;
        }
        if (modifyFlag){
            return Result.ok("成功");
        }else {
            return Result.error("失败");
        }
    }

    @PostMapping("/getUserList")
    private Result getUserList(@RequestBody JSONObject jsonObject) {
        JSONObject condition = jsonObject.getJSONObject("condition");
        Page<User> page = adminService.getUserByCondition(condition);
        if (page != null){
            return Result.ok("成功").data("page", page);
        }else {
            return Result.error("失败");
        }
    }

}
